package easy.独特的摩尔斯电码字;

import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;
/*
国际摩尔斯电码规定了一种标准编码，其中每个字母都映射到一系列点和短划线，如下所示：“a”映射到“.-”，“b”映射到“-...”，“c”映射到 “-。-。“， 等等。

为方便起见，下面给出了英文字母26个字母的完整表格：
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
现在，给定一个单词列表，每个单词可以写成每个字母的摩尔斯代码的串联。 例如，“cab”可以写成“-.-.-....-”，（这是串联“-.-。”“ -  ......”“.-”）。 我们称之为串联，即一个词的转换。
返回我们所有单词中不同变换的数量。
Example:
Input: words = ["gin", "zen", "gig", "msg"]
Output: 2
Explanation:
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."

There are 2 different transformations, "--...-." and "--...--.".
 */

public class Solution {
    /*
    将mose code 存放到map中
    根据每个单词的字符获取对应的code，在放进set
    放回set的size
     */
    public int uniqueMorseRepresentations(String[] words) {
        String[] code={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        HashMap<String,String> mose=new HashMap<String, String>();
        char begin='a';

        for (int i = 0; i < code.length; i++) {
            mose.put(String.valueOf(begin),code[i]);
            begin= (char) (begin+1);
        }
        HashSet<String> res=new HashSet<String>();
        for (int i = 0; i < words.length; i++) {
            words[i]=words[i].toLowerCase();
            char[] w=words[i].toCharArray();
            StringBuilder stringBuilder=new StringBuilder();
            for (int j = 0; j < w.length; j++) {
                String c=mose.get(String.valueOf(w[j]));
                stringBuilder.append(c);
            }
            res.add(stringBuilder.toString());
        }
        return res.size();
    }
    @Test
    public void  test1(){
        Solution solution=new Solution();
        int size=solution.uniqueMorseRepresentations(new String[]{"gin", "zen", "gig", "msg"});
        System.out.println(size);
    }
}
